package com.skt.test.db;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DbAlarmAdapter {
	public static final String KEY_ROWID = "_id";
	public static final String KEY_ACTION = "action";
	public static final String KEY_CLASS = "class";
	public static final String KEY_TRIGGER = "trigger";
	public static final String KEY_INTERVAL = "interval";
	public static final String KEY_TYPE = "type";
	
	private static final String TAG = "PAS";
	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDb;
	
	private static final String DATABASE_CREATE =
			"create table alarm(_id INTEGER PRIMARY KEY AUTOINCREMENT,"+
		                        "action TEXT NOT NULL,"+
		                        "class INTEGER NOT NULL,"+
		                        "trigger INTEGER NOT NULL,"+
		                        "interval INTEGER,"+
		                        "type INTEGER NOT NULL);";
	
	private static final String DATABASE_NAME = "alarm.db";
	private static final String DATABASE_TABLE = "alarm";
	private static final int DATABASE_VERSION = 1;
	
	private Context mContext;
	
	private class DatabaseHelper extends SQLiteOpenHelper{
		public DatabaseHelper(Context context){
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			db.execSQL(DATABASE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			Log.w(TAG, "Upgrading db from version" + oldVersion + " to" +
					newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS data");
			onCreate(db);
		}
	}
	
	public DbAlarmAdapter(Context mContext){
		this.mContext = mContext;
	}
	
	public DbAlarmAdapter open() throws SQLException{
		mDbHelper = new DatabaseHelper(mContext);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}
	
	public void close(){
		mDbHelper.close();
	}
	
	public void beginTransaction(){
		mDb.beginTransaction();
	}
	
	public void endTransaction(){
		mDb.endTransaction();
	}
	
	public void setTransactionSuccessful(){
		mDb.setTransactionSuccessful();
	}
	
	public int insertAlarm(Map param){
		
		int id = 0;
		ContentValues values = new ContentValues();
		
		values.put("action", 	(String) param.get("action"));
		values.put("class", 	(String) param.get("class"));
		values.put("trigger", 	(String) param.get("trigger"));
		values.put("interval", 	(String) param.get("interval"));
		values.put("type", 		(String) param.get("type"));
		id = (int) mDb.insertWithOnConflict(DATABASE_TABLE, null, values, SQLiteDatabase.CONFLICT_IGNORE);
		Log.d(TAG, String.valueOf(id) + " INSERT");
		
		return id;
	}
	
	public List<Map<String, String>> selectAlarmByAction(String action){

		List<Map<String, String>> returnList = new ArrayList<Map<String, String>>();
		
		
		Cursor cursor = mDb.query(DATABASE_TABLE, 
				new String[]{"_id", "action", "class", "trigger", "interval", "type"}
				, "action=?",new String[]{action}, null, null, null);
		
		while(cursor.moveToNext()){
			Map<String, String> resultMap = new HashMap<String, String>();
			resultMap.put("_id", cursor.getString(0));
			resultMap.put("action", cursor.getString(1));
			resultMap.put("class", cursor.getString(2));
			resultMap.put("trigger", cursor.getString(3));
			resultMap.put("interval", cursor.getString(4));
			resultMap.put("type", cursor.getString(5));
			
			returnList.add(resultMap);
		}
		
		cursor.close();
		
		return returnList;
	}
	
	public List<Map<String, String>> selectAllAlarm(){
		
		List<Map<String, String>> returnList = new ArrayList<Map<String, String>>();
		
		Cursor cursor = mDb.query(DATABASE_TABLE, 
				new String[]{"_id", "action", "class", "trigger", "interval", "type"}
				, null, null, null, null, null);
		
		while(cursor.moveToNext()){
			Map<String, String> resultMap = new HashMap<String, String>();
			resultMap.put("_id", cursor.getString(0));
			resultMap.put("action", cursor.getString(1));
			resultMap.put("class", cursor.getString(2));
			resultMap.put("trigger", cursor.getString(3));
			resultMap.put("interval", cursor.getString(4));
			resultMap.put("type", cursor.getString(5));
			
			returnList.add(resultMap);
		}
		
		cursor.close();
		
		return returnList;
	}
	
	public int deleteAlarmByAction(String action){
		int returnValue = 0;
		returnValue = mDb.delete(DATABASE_TABLE, "action=?", new String[]{ action });
		return returnValue;
	}
	
	public int deleteAlarmById(String id){
		int returnValue = 0;
		returnValue = mDb.delete(DATABASE_TABLE, "_id=?", new String[]{ id });
		return returnValue;
	}
	
	public int deleteAlarmAll(){
		int returnValue = 0;
		returnValue = mDb.delete(DATABASE_TABLE, null, null);
		return returnValue;
	}
}
